/*
 * Created on Jun 6, 2005
 *
 */
package com.erclab.internal.xpresso.reports;

import java.util.*;

import com.erclab.internal.xpresso.Resource;
import com.erclab.internal.xpresso.exceptions.ReportColumnNotFound;
import com.erclab.internal.xpresso.reportelements.ReportElement;


/**
 * @author Enrique Rodriguez.
 *
 */
public class ReportResultSet extends Resource{

	protected Report theReport;
	private HashMap<String, Integer> columnNameToKey;
	protected ArrayList<String[]> rows;
	protected ArrayList<String> rowUniqueKeyValue;
	protected int index;
	protected int indexUniqueKey;
	protected int maxNumberOfResults;
	
	/**
	 * 
	 */
	public ReportResultSet(Report r) {
		theReport = r;
		index = -1;
		this.rows = new ArrayList<String[]>();
		this.columnNameToKey = new HashMap<String, Integer>();
		this.rowUniqueKeyValue = new ArrayList<String>();
		
		for (int i =0; i < r.getReportElements().size(); i++){
			ReportElement re = ((ReportElement)r.getReportElements().get(i));
			columnNameToKey.put(re.getColumnName(), new Integer(i));
			
			if(re.getColumnName() != null && re.getColumnName().equals(r.getUniqueKeyName())){
				indexUniqueKey = i;
			}
		}
	}

	public String getHeaderText(int i) {
		return ((ReportElement)theReport.getReportElements().get(i)).getHeaderText();
	}

	public String getHeaderText(String columnName)throws ReportColumnNotFound {
		return ((ReportElement)theReport.getReportElements().
				get(this.getColumnIndex(columnName))).getHeaderText();
	}
	
	public void addRow(String[] row, String uniqueKeyValue){
		this.rows.add(row);
		this.rowUniqueKeyValue.add(uniqueKeyValue);
	}
	
	public int getNumberOfResults(){
		return rows.size(); 
	}

	public boolean nextRow(){
		index++;
		if (index < rows.size())
			return true;
		else
			return false;		
	}
	
	public String getRowValue(int i){
		return rows.get(index)[i];
	}
	
	public String getRowValue(String columnName) throws ReportColumnNotFound{
		return rows.get(index)[getColumnIndex(columnName)];
	}
	
	public int getColumnIndex(String columnName) throws ReportColumnNotFound{
		Integer indexPos = columnNameToKey.get(columnName);
		if(indexPos == null)
			throw new ReportColumnNotFound("The column: " + columnName + " was not found in the resultset.");
		return indexPos.intValue();
	}
	
	public String getRowUniqueKeyValue(){
		return rowUniqueKeyValue.get(index);
	}
	
	/**
	 * @return
	 */
	public String getColumnStyle(int i) {
		return ((ReportElement)theReport.getReportElements().get(i)).getStyle();
	}

	/**
	 * @return
	 */
	public int getNumberOfColumns() {
		return theReport.getReportElements().size();
	}

	/**
	 * @return
	 */
	public String getColumnName(int i) {
		return ((ReportElement)theReport.getReportElements().get(i)).getColumnName();
	}

	/**
	 * @return
	 */
	public String getIdReportElement(int i) {
		return ((ReportElement)theReport.getReportElements().get(i)).getIdReportElement();
	}

	/**
	 * @return
	 */
	public boolean getIsTotalColumn(int i) {
		return ((ReportElement)theReport.getReportElements().get(i)).getIsTotalColumn();
	}

	/**
	 * @return
	 */
	public int getMaxNumberOfResults() {
		return maxNumberOfResults;
	}

	/**
	 * @param i
	 */
	public void setMaxNumberOfResults(int i) {
		maxNumberOfResults = i;
	}
	
	/**
	 * @deprecated
	 */
	public void moveFirst(){
		index = 0;
	}
	
	public void movePrevious(){
		index = index - 1;
	}
	
	public void moveBeforeFirst(){
		index = -1;
	}
	

}
